<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <link rel="stylesheet" href="./assets/news.css" />
</head>

<body>
    <div id="news-list">
        <!-- <div class="news-item">
            <img class="thumb" src="http://www.liulongbin.top:3006/images/2.webp" alt="">
            <div class="right-box">
                <h1 class="title">华为5G随行WiFi发布，5G布局全面领先苹果、三星</h1>
                <div class="tags">
                    <span>华为</span>
                    <span>苹果</span>
                    <span>5G网络</span>
                </div>
                <div class="footer">
                    <div>
                        <span>驱动中国网络传媒</span>&nbsp;&nbsp;
                        <span>2019-10-26 16:51:30</span>
                    </div>
                    <span>评论数：32</span>
                </div>
            </div>
        </div> -->
    </div>

    <script src="./lib/jquery.js"></script>
    <script src="./lib/axios.js"></script>
    <script>
        // 接口文档：https://www.showdoc.com.cn/1834761734600444/8449563515660414
        //  在 公共接口中的 01-获取新闻列表


        axios.defaults.baseURL = 'http://www.liulongbin.top:3006/'
        /* 
        axios({
            method: 'get',
            url: '/api/news',
        }).then(({data: res}) => {
            console.log(res.data);
        }) */

        async function fn() {
            const {
                data: res
            } = await axios.get('/api/news')
            console.log(res.data);
            const htmlSTr = res.data.map((item, index) => {
                console.log(item.tags); //字符串
                console.log(item.tags.split(',')); //转成数组
               
                return `
                  <div class="news-item">
                      <img class="thumb" src="http://www.liulongbin.top:3006${item.img}" alt="">
                      <div class="right-box">
                          <h1 class="title">${item.title}</h1>
                          <div class="tags">
                             ${rendertags(item.tags)}
                          </div>
                          <div class="footer">
                              <div>
                                  <span>${item.source}</span>&nbsp;&nbsp;
                                  <span>${formatTime(item.time)}</span>
                              </div>
                              <span>评论数：${item.cmtcount}</span>
                          </div>
                      </div>
                  </div>
        `
            }).join("")
            document.querySelector('#news-list').innerHTML = htmlSTr
        }
        fn()

        function rendertags(tags) {
            return tags.split(',').map(ele => {
                return `<span>${ele}</span>`
            })
        }

        //时间补零
        function formatTime(time) {
            let d = new Date(time)
            // 处理时间格式
            let year = parseInt(d.getFullYear());
            let month = parseInt(d.getMonth() + 1);
            let day = parseInt(d.getDate());
            let h = parseInt(d.getHours());
            let m = parseInt(d.getMinutes());
            let s = parseInt(d.getSeconds());
            //返回时间
            return `${year}-${month}-${day} ${h}:${m}:${s}`
        }
    </script>
</body>

</html>